﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MediaBrowser.CustomControls.CustomEvent;

namespace MediaBrowser.CustomControls.Panels
{
    public class ClosableControl : UserControl, INotifyEventControl
    {
        private bool isMinimize;

        public ClosableControl()
        {
            isMinimize = true;
        }

        #region INotifyClosedControl Membres

        public void Close_Event(object sender, MouseButtonEventArgs e)
        {
            if (OnControlClosed != null)
            {
                OnControlClosed(this);
            }
            CloseEventArgs args = new CloseEventArgs(CloseEvent, this);
            RaiseEvent(args);
        }

        public void Size_Event(object sender, MouseButtonEventArgs e)
        {
            if (isMinimize)
            {
                if (OnControlMaximize != null)
                {
                    OnControlMaximize(this);
                }
                GenericEventArgs args = new GenericEventArgs(MaximizeEvent, this);
                RaiseEvent(args);

                isMinimize = false;
            }
            else
            {
                if (OnControlMinimize != null)
                {
                    OnControlMinimize(this);
                }
                GenericEventArgs args = new GenericEventArgs(MinimizeEvent, this);
                RaiseEvent(args);

                isMinimize = true;
            }
        }

        #endregion

        public event CloseEvent OnControlClosed;
        public event GenericEvent OnControlMinimize;
        public event GenericEvent OnControlMaximize;

        #region Event

        public static readonly RoutedEvent CloseEvent =
          EventManager.RegisterRoutedEvent(
          "OnClose", RoutingStrategy.Bubble,
          typeof(CloseEventHandler),
          typeof(ClosableControl));

        public event CloseEventHandler OnClose
        {
            add { AddHandler(CloseEvent, value); }
            remove { RemoveHandler(CloseEvent, value); }
        }

        public static readonly RoutedEvent MinimizeEvent =
          EventManager.RegisterRoutedEvent(
          "OnMinimize", RoutingStrategy.Bubble,
          typeof(GenericEventHandler),
          typeof(ClosableControl));

        public event GenericEventHandler OnMinimize
        {
            add { AddHandler(MaximizeEvent, value); }
            remove { RemoveHandler(MaximizeEvent, value); }
        }

        public static readonly RoutedEvent MaximizeEvent =
         EventManager.RegisterRoutedEvent(
         "OnMaximize", RoutingStrategy.Bubble,
         typeof(GenericEventHandler),
         typeof(ClosableControl));

        public event GenericEventHandler OnMaximize
        {
            add { AddHandler(MaximizeEvent, value); }
            remove { RemoveHandler(MaximizeEvent, value); }
        }


        #endregion
    }
}
